package com.eml.service.dao.impl;

import com.eml.service.dao.MenuDao;
import com.eml.service.entity.Menu;
import com.eml.service.util.JDBCUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class MenuDaoImpl implements MenuDao {
    @Override
    public Menu selectMenuByMenu_Id(Integer menuId) throws Exception {
        Connection connection = JDBCUtils.getConnection();
        String sql = "select menu_id,menu_quantity,menu_note,menu_remain,menu_name from menu where menu_id = ?";
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        preparedStatement.setInt(1,menuId);
        ResultSet resultSet = preparedStatement.executeQuery();
        if (resultSet.next()){
            int id = resultSet.getInt("menu_id");
            int quantity = resultSet.getInt("menu_quantity");
            String note = resultSet.getString("menu_note");
            int remain = resultSet.getInt("menu_remain");
            String name = resultSet.getString("menu_name");
            JDBCUtils.close(connection,preparedStatement,resultSet);
            return new Menu(id,quantity,note,remain,name);

        }
        JDBCUtils.close(connection,preparedStatement,resultSet);
        return null;


    }

    @Override
    public int updateMenuQuantity(Integer menuQuantity, String menuName) throws Exception {
        Connection connection = JDBCUtils.getConnection();
        String sql = "update menu set menu_quantity = ? where menu_name = ?";
            PreparedStatement preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setInt(1, menuQuantity);
            preparedStatement.setString(2, menuName);
            return preparedStatement.executeUpdate();



    }

    @Override
    public void updateMenuNote(String menuNote, String menuName) throws Exception {
        Connection connection = JDBCUtils.getConnection();
        String sql = "update menu set menu_note = ? where menu_name = ?";
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        preparedStatement.setString(1,menuNote);
        preparedStatement.setString(2,menuName);
        preparedStatement.executeUpdate();
        JDBCUtils.close(connection,preparedStatement);

    }

    @Override
    public int updateMenuRemain(Integer menuRemain, String menuName) throws Exception {
        Connection connection = JDBCUtils.getConnection();
        String sql = "update menu set menu_remain = ? where menu_name = ?";
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        preparedStatement.setInt(1, menuRemain);
        preparedStatement.setString(2, menuName);
        return preparedStatement.executeUpdate();

    }


    @Override
    public void insertMenu_Name(Menu menu) throws Exception {
        Connection connection = JDBCUtils.getConnection();
        String sql = "insert into menu(menu_quantity,menu_note,menu_remain,menu_name) values(?,?,?,?)";
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        preparedStatement.setInt(1, menu.getMenuQuantity());
        preparedStatement.setString(2, menu.getMenuNote());
        preparedStatement.setInt(3, menu.getMenuRemain());
        preparedStatement.setString(4, menu.getMenuName());
        preparedStatement.executeUpdate();
        JDBCUtils.close(connection,preparedStatement);
    }
}
